from fastapi import APIRouter, Request, Body, Query
from typing import Optional
from pydantic import BaseModel
from finance.python.middleware.auth import token_required
from finance.python.middleware.logging import log_required
from finance.python.services import notification_service

# 创建路由器
notification_router = APIRouter(
    prefix="/api",
    tags=["notification"]
)

class MessageRequest(BaseModel):
    message: str

class HandleInviteRequest(BaseModel):
    notification_id: int
    accept: bool

@notification_router.get("/get_notifications", summary="查询通知")
@token_required
@log_required
async def get_notifications(    
    request: Request,
    current_page: Optional[int] = Query(1, description="当前页码"),
    page_size: Optional[int] = Query(10, description="每页数量")
):
    """
    查询通知
    :param request: 请求对象
    :param current_page: 当前页码
    :param page_size: 每页数量
    :return: 通知列表
    """
    user_id = request.state.user_id
    return await notification_service.get_notifications(user_id, current_page, page_size)

@notification_router.post("/send_family_message", summary="发送家庭消息")
@token_required
@log_required
async def send_family_message(request: Request, message_data: MessageRequest = Body(...)):
    """
    发送家庭消息
    :param request: 请求对象
    :param message_data: 消息数据
    :return: 发送结果
    """
    user_id = request.state.user_id
    return await notification_service.send_family_message(user_id, message_data.message)

@notification_router.get("/check_new_messages", summary="检查新消息")
@token_required
@log_required
async def check_new_messages(request: Request):
    """
    检查新消息
    :param request: 请求对象
    :return: 检查结果
    """
    user_id = request.state.user_id
    return await notification_service.check_new_messages(user_id)

@notification_router.post("/handle_invite", summary="处理家庭邀请")
@token_required
@log_required
async def handle_invite(request: Request, data: HandleInviteRequest):
    """
    处理家庭邀请
    :param request: 请求对象
    :param data: 请求数据
    :return: 处理结果
    """
    user_id = request.state.user_id
    return await notification_service.handle_invite(user_id, data.notification_id, data.accept)

@notification_router.post("/handle_Join", summary="处理家庭邀请")
@token_required
@log_required
async def handle_join(request: Request, data: HandleInviteRequest):
    """
    处理申请加入家庭
    :param request: 请求对象
    :param data: 请求数据
    :return: 处理结果
    """
    user_id = request.state.user_id
    return await notification_service.handle_join(user_id, data.notification_id, data.accept)
